package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.vo.FormDataAddGetVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.businessdata.pojo.dto.HasCommunicatePlanDTO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.fromdatadictionary.pojo.vo.DictionaryArrayVO;
import com.xbongbong.pro.weblist.pojo.dto.CommunicatePlanDTO;
import com.xbongbong.pro.weblist.pojo.dto.CommunicatePlanOperateDTO;
import com.xbongbong.pro.weblist.pojo.dto.CommunicatePlanSearchDTO;
import com.xbongbong.pro.weblist.pojo.dto.DelayCauseDTO;
import com.xbongbong.pro.weblist.pojo.dto.SaasBaseDTO;
import com.xbongbong.pro.weblist.pojo.vo.CommunicatePlanDayListVO;
import com.xbongbong.pro.weblist.pojo.vo.CommunicatePlanSearchVO;
import com.xbongbong.pro.weblist.pojo.vo.CommunicatePlanStatisicsVO;
import com.xbongbong.pro.weblist.pojo.vo.CustomerPlanVO;
import com.xbongbong.pro.weblist.pojo.vo.SpecialListFilterVO;
import com.xbongbong.saas.service.CommunicatePlanService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/2/1 11:11
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/communicatePlan")
public class CommunicatePlanController {
    @Resource
    private CommunicatePlanService communicatePlanService;

    /**
     * 当前月有访客计划的列表
     * @param communicatePlanDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/dayList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String dayList(@RequestBody @Valid CommunicatePlanDTO communicatePlanDTO, BindingResult br) {
        XbbResponse<CommunicatePlanDayListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(communicatePlanDTO.getSaasMark(), communicatePlanDTO.getBusinessType(), null, communicatePlanDTO.getDistributorMark());
                CommunicatePlanDayListVO communicatePlanDayListVO = communicatePlanService.dayList(communicatePlanDTO);
                response = new XbbResponse<>(communicatePlanDayListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 当前日期访客计划统计
     * @param communicatePlanDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/statistics", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String statistics(@RequestBody @Valid CommunicatePlanDTO communicatePlanDTO, BindingResult br) {
        XbbResponse<CommunicatePlanStatisicsVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CommunicatePlanStatisicsVO communicatePlanStatisicsVO = communicatePlanService.statistics(communicatePlanDTO);
                response = new XbbResponse<>(communicatePlanStatisicsVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取访客计划数据列表
     * @param communicatePlanDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String customerPlanList(@RequestBody @Valid CommunicatePlanDTO communicatePlanDTO, BindingResult br) {
        XbbResponse<CustomerPlanVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(communicatePlanDTO.getSaasMark(), communicatePlanDTO.getBusinessType(), null, communicatePlanDTO.getDistributorMark());
                CustomerPlanVO customerPlanVO = communicatePlanService.list(communicatePlanDTO);
                response = new XbbResponse<>(customerPlanVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

     /**
    　　* @description: 根据客户名称或客户电话进行模糊查询返回客户列表
        * @param communicatePlanSearchDTO
        * @param br
    　　* @return java.lang.String
    　　* @author zhushuainan
    　　* @date 2022/03/11 14:09:26
    　　*/
    @RequestMapping(value = "/get/fuzzyQueryList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getFuzzyQueryList(@RequestBody @Valid CommunicatePlanSearchDTO communicatePlanSearchDTO, BindingResult br) throws Exception {
        XbbResponse<CommunicatePlanSearchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CommunicatePlanSearchVO communicatePlanSearchVO = communicatePlanService.getFuzzyQueryList(communicatePlanSearchDTO);
                response = new XbbResponse<>(communicatePlanSearchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取访客计划状态筛选
     * @param saasBaseDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/filter/specialList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String statusSearch(@RequestBody @Valid SaasBaseDTO saasBaseDTO, BindingResult br) {
        XbbResponse<SpecialListFilterVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SpecialListFilterVO specialListFilterVO = communicatePlanService.statusSearch(saasBaseDTO);
                response = new XbbResponse<>(specialListFilterVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取访客计划状态筛选
     * @param communicatePlanOperateDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/singleOperate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String singleOperate(@RequestBody @Valid CommunicatePlanOperateDTO communicatePlanOperateDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = communicatePlanService.singleOperate(communicatePlanOperateDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取访客计划状态筛选
     * @param delayCauseDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/getDelayCauseDataDictionary", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String singleOperate(@RequestBody @Valid DelayCauseDTO delayCauseDTO, BindingResult br) {
        XbbResponse<DictionaryArrayVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DictionaryArrayVO dictionaryArrayVO = communicatePlanService.getDelayCauseDataDictionary(delayCauseDTO);
                response = new XbbResponse<>(dictionaryArrayVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取是否有访客计划可完成
     * @param businessDataDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/hasPlanFinish", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String hasPlanFinish(@RequestBody @Valid HasCommunicatePlanDTO hasCommunicatePlanDTO, BindingResult br) {
        XbbResponse<FormDataAddGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormDataAddGetVO formDataAddGetVO = communicatePlanService.hasPlanFinish(hasCommunicatePlanDTO);
                response = new XbbResponse<>(formDataAddGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
